<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="启用单设备登录" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="[ 'enableSingleDeviceLogin', validatorRules.enableSingleDeviceLogin]" >
            <a-radio v-for="(item, key) in statusList" :key="key" :value="item.value">{{ item.title }}</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="总经理银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'bossBankNumber', validatorRules.bossBankNumber]" placeholder="请输入总经理银行卡号"></a-input>
        </a-form-item>
        <a-form-item label="总经理银行卡类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'bossBankType', validatorRules.bossBankType]" placeholder="请输入总经理银行卡类型"></a-input>
        </a-form-item>
        <a-form-item label="总经理邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'bossEmail', validatorRules.bossEmail]" placeholder="请输入总经理邮箱"></a-input>
        </a-form-item>
        <a-form-item label="数据库备份邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mysqlBackEmail', validatorRules.mysqlBackEmail]" placeholder="请输入数据库备份邮箱"></a-input>
        </a-form-item>
        <a-form-item label="总经理登录名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'boosUsername', validatorRules.boosUsername]" placeholder="请输入总经理登录名"></a-input>
        </a-form-item>
        <a-form-item label="apk下载地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'apkDownloadUrl', validatorRules.apkDownloadUrl]" placeholder="请输入apk下载地址"></a-input>
        </a-form-item>
        <a-form-item label="文件预览地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'fileViewUrl', validatorRules.fileViewUrl]" placeholder="请输入文件预览地址"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: "CommonConfigModal",
    components: {
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        delFlag:{},
        enableSingleDeviceLogin:{rules:[{required:true,message :'请选择费否启用单设备登录！'}]},
        bossBankNumber:{},
        bossBankType:{},
        bossEmail:{},
        mysqlBackEmail:{},
        boosUsername:{},
        apkDownloadUrl:{},
        fileViewUrl:{},
        },
        url: {
          add: "/commonconfig/commonConfig/add",
          edit: "/commonconfig/commonConfig/edit",
        },
        statusList:[
          {
            title:'启用',
            value:'yes'
          },
          {
            title:'不启用',
            value:'no'
          }
        ]

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'enableSingleDeviceLogin','bossBankNumber','bossBankType','bossEmail','mysqlBackEmail','boosUsername','apkDownloadUrl','fileViewUrl'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },

      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'delFlag','enableSingleDeviceLogin','bossBankNumber','bossBankType','bossEmail','mysqlBackEmail','boosUsername','apkDownloadUrl','fileViewUrl'))
      },


    }
  }
</script>